<template>
  <a-card :bordered="false" class="card-area" padding-bottom="53px" bottom="auto" :closable="false"
          @close="onClose" :editVisiable="false">
    <a-form :form="form" :mca00="mca00">
      <a-form-item label='快递名称' v-bind="formItemLayout">
        <a-input v-decorator="['mca01',{rules: [
        { required: true, message: '快递名称不能为空'},
        {pattern:'^[\u4e00-\u9fa5\\\\w]+$',message:'请输入中文'},
        ]}]" />
      </a-form-item>
      <a-form-item label='英文编码' v-bind="formItemLayout">
        <a-input v-decorator="['mca02',{rules: [
        { required: true, message: '英文编码不能为空'},
        {pattern:'^[A-Z]+$',message:'请输入大写英文字母'},
        ]}]" />      </a-form-item>
    </a-form>
    <div class="bootom-button" style="float:right;">
      <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
    </div>
  </a-card>
</template>
<script>
const formItemLayout = {
  labelCol: {span: 6},
  wrapperCol: {span: 18}
}
export default {
  name: 'McaEdit',
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      mca00: ''
    }
  },
  created () {
    const bind = arr => data => arr.reduce((r1, r2) => {
      r1[r2] = this.$form.createFormField({
        value: data[r2]
      })
      return r1
    }, {})
    this.$api.freight.getMca(this.$route.query.id).then((r) => { //  获取接口mca对象和等等的所有数据
      console.log(r)
      let data = r.data.data
      this.mca00 = data.mca00
      this.form = this.$form.createForm(this, {
        mapPropsToFields: () => {
          return bind(['mca01', 'mca02'])(data)
        }
      })
      console.log(data)
    })
  },
  methods: {
    // 清空
    reset () {
      this.loading = false
      this.form.resetFields()
    },
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 修改提交
    handleSubmit () {
      this.form.validateFields((err, values) => {
        values.mca00 = this.mca00
        if (!err) {
          console.log(values)
          this.$api.freight.putMca(values).then(() => {
            this.$emit('success')
            this.$message.success('修改快递模板成功')
            this.reset()
            this.$router.push({path: `mca`})
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
